package com.example.demo.jenkins;

import com.example.demo.pojo.ModelFactory;
import com.example.demo.utils.LoggerConfig;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Computer;

import java.io.IOException;
import java.util.Map;

public class JenkinsApi {

    private JenkinsServer jenkinsServer;

    private JenkinsApi(){
        jenkinsServer = ModelFactory.INSTANCE.getJenkinsServer();
    }

    private void getMasterInfo(){
        try {
            Map<String, Computer> info= jenkinsServer.getComputers();
            for (Computer computer : info.values()) {
                // 获取节点名称
                LoggerConfig.getLoggerInfo("------------"+computer.details().getDisplayName());;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重启Jenkins
     */
    private void restart(){
        try {
            jenkinsServer.restart(true);
        } catch (IOException e) {
            LoggerConfig.getLoggerError("重启失败");
            e.printStackTrace();
        }
    }

    /**
     * 关闭Jenkins连接
     */
    private void closeJenkinsConnection(){
        jenkinsServer.close();
    }

    public static void main(String[] args) {
        JenkinsApi jenkinsApi = new JenkinsApi();
        jenkinsApi.getMasterInfo();
    }

}
